package com.xgimi.open.skill.datamanager.biz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.xgimi.open.skill.datamanager.biz.service.EntryBizService;
import com.xgimi.open.skill.datamanager.domain.entity.EntryDO;
import com.xgimi.open.skill.datamanager.domain.service.EntryService;
import com.xgimi.open.skill.datamanager.service.request.EntryReqDTO;
import com.xgimi.open.skill.datamanager.service.response.EntryRespDTO;
import com.xgimi.open.skill.datamanager.service.response.page.IPageUtils;
import com.xgimi.open.skill.datamanager.service.response.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author coderzpw.zhang
 * @version 1.0
 * @description 描述
 * @date 2023/4/13 11:15
 */
@Slf4j
@Service
public class    EntryBizServiceImpl implements EntryBizService {
    @Autowired
    private EntryService entryService;

    @Override
    public List<EntryRespDTO> listByWordStockIds(List<Long> wordStockIds) {
        List<EntryDO> entryDOS = entryService.listByWordStockIds(wordStockIds);
        List<EntryRespDTO> ret = entryDOS.stream().map(x -> {
            EntryRespDTO entryRespDTO = new EntryRespDTO();
            BeanUtils.copyProperties(x, entryRespDTO);
            return entryRespDTO;
        }).collect(Collectors.toList());
        return ret;
    }

    @Override
    public PageResult<EntryRespDTO> pageQuery(EntryReqDTO entryReqDTO) {
        EntryDO entryDO = new EntryDO();
        BeanUtils.copyProperties(entryReqDTO, entryDO);
        // 构造分页查询的入参
        IPage<EntryDO> page = new Page<>(entryReqDTO.getCurrentPage(), entryReqDTO.getPageSize());
        IPage<EntryDO> iPage = entryService.pageQuery(page, entryDO);
        // 若查询没结果，则返回空pageResult
        if (CollectionUtils.isEmpty(iPage.getRecords())) {
            log.info("no such wordStock!");
            return new PageResult<>(Lists.newArrayList(), IPageUtils.noDataPaginator(entryReqDTO.getPageSize()));
        }
        List<EntryRespDTO> list = iPage.getRecords().stream().map(x -> {
            EntryRespDTO entryRespDTO = new EntryRespDTO();
            BeanUtils.copyProperties(x, entryRespDTO);
            return entryRespDTO;
        }).collect(Collectors.toList());
        return new PageResult<>(list, IPageUtils.transformToPaginator(iPage));
    }
}
